function prefix(n) {
    const str = '' + n;
    if (str.length < 2) {
        return '0' + str;
    }
    return str;
}
export function formatTime(time: any = new Date(), format = 'yyyy-mm-dd HH:MM:SS') {
    if (time == null) return "N/A"
    const t = new Date(time);
    const y = t.getFullYear();
    const m = t.getMonth();
    const d = t.getDate();
    const H = t.getHours();
    const M = t.getMinutes();
    const S = t.getSeconds();
    const s = t.getMilliseconds();
    return format.replace('yyyy', prefix(y))
        .replace('mm', prefix(m + 1))
        .replace('dd', prefix(d))
        .replace('HH', prefix(H))
        .replace('MM', prefix(M))
        .replace('SS', prefix(S))
        .replace('MS', prefix(s))
}
var CHAR_FORWARD_SLASH = 47         //'/'
var CHAR_BACKWARD_SLASH = 92        //'\'
var CHAR_DOT = 46                   //'.'
var CHAR_COLON = 58                 //':'
var CHAR_UPPERCASE_A = 65
var CHAR_UPPERCASE_Z = 90
var CHAR_LOWERCASE_A = 97
var CHAR_LOWERCASE_Z = 122

function isPathSeparator(code) {
    return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
}

function isPosixPathSeparator(code) {
    return code === CHAR_FORWARD_SLASH;
}

function isWindowsDeviceRoot(code) {
    return (code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) ||
        (code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z);
}

// Resolves . and .. elements in a path with directory names
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
    let res = '';
    let lastSegmentLength = 0;
    let lastSlash = -1;
    let dots = 0;
    let code = 0;
    for (let i = 0; i <= path.length; ++i) {
        if (i < path.length)
            code = path.charCodeAt(i);
        else if (isPathSeparator(code))
            break;
        else
            code = CHAR_FORWARD_SLASH;

        if (isPathSeparator(code)) {
            if (lastSlash === i - 1 || dots === 1) {
                // NOOP
            } else if (dots === 2) {
                if (res.length < 2 || lastSegmentLength !== 2 ||
                    res.charCodeAt(res.length - 1) !== CHAR_DOT ||
                    res.charCodeAt(res.length - 2) !== CHAR_DOT) {
                    if (res.length > 2) {
                        const lastSlashIndex = res.lastIndexOf(separator);
                        if (lastSlashIndex === -1) {
                            res = '';
                            lastSegmentLength = 0;
                        } else {
                            res = res.slice(0, lastSlashIndex);
                            lastSegmentLength =
                                res.length - 1 - res.lastIndexOf(separator);
                        }
                        lastSlash = i;
                        dots = 0;
                        continue;
                    } else if (res.length !== 0) {
                        res = '';
                        lastSegmentLength = 0;
                        lastSlash = i;
                        dots = 0;
                        continue;
                    }
                }
                if (allowAboveRoot) {
                    res += res.length > 0 ? `${separator}..` : '..';
                    lastSegmentLength = 2;
                }
            } else {
                if (res.length > 0)
                    res += `${separator}${path.slice(lastSlash + 1, i)}`;
                else
                    res = path.slice(lastSlash + 1, i);
                lastSegmentLength = i - lastSlash - 1;
            }
            lastSlash = i;
            dots = 0;
        } else if (code === CHAR_DOT && dots !== -1) {
            ++dots;
        } else {
            dots = -1;
        }
    }
    return res;
}

/**
 * @param {string} path
 * @returns {string}
 */
function win_normalize(path) {
    //validateString(path, 'path');
    const len = path.length;
    if (len === 0)
        return '.';
    let rootEnd = 0;
    let device;
    let isAbsolute = false;
    const code = path.charCodeAt(0);

    // Try to match a root
    if (len === 1) {
        // `path` contains just a single char, exit early to avoid
        // unnecessary work
        return isPosixPathSeparator(code) ? '\\' : path;
    }
    if (isPathSeparator(code)) {
        // Possible UNC root

        // If we started with a separator, we know we at least have an absolute
        // path of some kind (UNC or otherwise)
        isAbsolute = true;

        if (isPathSeparator(path.charCodeAt(1))) {
            // Matched double path separator at beginning
            let j = 2;
            let last = j;
            // Match 1 or more non-path separators
            while (j < len &&
                !isPathSeparator(path.charCodeAt(j))) {
                j++;
            }
            if (j < len && j !== last) {
                const firstPart = path.slice(last, j);
                // Matched!
                last = j;
                // Match 1 or more path separators
                while (j < len &&
                    isPathSeparator(path.charCodeAt(j))) {
                    j++;
                }
                if (j < len && j !== last) {
                    // Matched!
                    last = j;
                    // Match 1 or more non-path separators
                    while (j < len &&
                        !isPathSeparator(path.charCodeAt(j))) {
                        j++;
                    }
                    if (j === len) {
                        // We matched a UNC root only
                        // Return the normalized version of the UNC root since there
                        // is nothing left to process
                        return `\\\\${firstPart}\\${path.slice(last)}\\`;
                    }
                    if (j !== last) {
                        // We matched a UNC root with leftovers
                        device =
                            `\\\\${firstPart}\\${path.slice(last, j)}`;
                        rootEnd = j;
                    }
                }
            }
        } else {
            rootEnd = 1;
        }
    } else if (isWindowsDeviceRoot(code) &&
        path.charCodeAt(1) === CHAR_COLON) {
        // Possible device root
        device = path.slice(0, 2);
        rootEnd = 2;
        if (len > 2 && isPathSeparator(path.charCodeAt(2))) {
            // Treat separator following drive name as an absolute path
            // indicator
            isAbsolute = true;
            rootEnd = 3;
        }
    }

    let tail = rootEnd < len ?
        normalizeString(path.slice(rootEnd),
            !isAbsolute, '\\', isPathSeparator) :
        '';
    if (tail.length === 0 && !isAbsolute)
        tail = '.';
    if (tail.length > 0 &&
        isPathSeparator(path.charCodeAt(len - 1)))
        tail += '\\';
    if (device === undefined) {
        return isAbsolute ? `\\${tail}` : tail;
    }
    return isAbsolute ? `${device}\\${tail}` : `${device}${tail}`;
}



/**
 * @param {...string} args
 * @returns {string}
 */
function win_join(...args) {
    if (args.length === 0)
        return '.';

    let joined;
    let firstPart;
    for (let i = 0; i < args.length; ++i) {
        const arg = args[i];
        //validateString(arg, 'path');
        if (arg.length > 0) {
            if (joined === undefined)
                joined = firstPart = arg;
            else
                joined += `\\${arg}`;
        }
    }

    if (joined === undefined)
        return '.';

    // Make sure that the joined path doesn't start with two slashes, because
    // normalize() will mistake it for a UNC path then.
    //
    // This step is skipped when it is very clear that the user actually
    // intended to point at a UNC path. This is assumed when the first
    // non-empty string arguments starts with exactly two slashes followed by
    // at least one more non-slash character.
    //
    // Note that for normalize() to treat a path as a UNC path it needs to
    // have at least 2 components, so we don't filter for that here.
    // This means that the user can use join to construct UNC paths from
    // a server name and a share name; for example:
    //   path.join('//server', 'share') -> '\\\\server\\share\\')
    let needsReplace = true;
    let slashCount = 0;
    if (isPathSeparator(firstPart.charCodeAt(0))) {
        ++slashCount;
        const firstLen = firstPart.length;
        if (firstLen > 1 &&
            isPathSeparator(firstPart.charCodeAt(1))) {
            ++slashCount;
            if (firstLen > 2) {
                if (isPathSeparator(firstPart.charCodeAt(2)))
                    ++slashCount;
                else {
                    // We matched a UNC path in the first part
                    needsReplace = false;
                }
            }
        }
    }
    if (needsReplace) {
        // Find any more consecutive slashes we need to replace
        while (slashCount < joined.length &&
            isPathSeparator(joined.charCodeAt(slashCount))) {
            slashCount++;
        }

        // Replace the slashes if needed
        if (slashCount >= 2)
            joined = `\\${joined.slice(slashCount)}`;
    }

    return win_normalize(joined);
}
/**
 * @param {string} path
 * @returns {string}
 */
function linux_normalize(path) {
    //validateString(path, 'path');

    if (path.length === 0)
        return '.';

    const isAbsolute =
        path.charCodeAt(0) === CHAR_FORWARD_SLASH;
    const trailingSeparator =
        path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;

    // Normalize the path
    path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);

    if (path.length === 0) {
        if (isAbsolute)
            return '/';
        return trailingSeparator ? './' : '.';
    }
    if (trailingSeparator)
        path += '/';

    return isAbsolute ? `/${path}` : path;
}


/**
 * @param {...string} args
 * @returns {string}
 */
function linux_join(...args) {
    if (args.length === 0)
        return '.';
    let joined;
    for (let i = 0; i < args.length; ++i) {
        const arg = args[i];
        //validateString(arg, 'path');
        if (arg.length > 0) {
            if (joined === undefined)
                joined = arg;
            else
                joined += `/${arg}`;
        }
    }
    if (joined === undefined)
        return '.';
    return linux_normalize(joined);
}
export function getRealPath(filepath, name, type = 'Local') {
    let isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows")
    if (type == 'Local' && isWin) {
        return win_join(filepath, name);
    } else {
        return linux_join(filepath, name);
    }
}